<?php
class Helper
{
    public static function getUrl($url)
    {
        return Yii::app()->request->baseUrl . '/' . trim($url, '/');
    }
    
    public static function summary($text, $tag = '</p>')
    {
    	return substr_replace($text, '....', strpos($text, $tag));
    }
    
    /**
     * get config param from path of application/config/params.php
     * @param string $name
     * @return mixed
     */
    public static function getConfigParam($name)
    {
        return isset(Yii::app()->params[$name]) ? Yii::app()->params[$name] : null;
    }
    
    /**
     * get css, js, image path
     * @param string $fileName name of css, js, image
     * @param string $extension extention
     */
    public static function getClientFile($fileName, $extension = null)
    {
        switch($extension){
            case 'css':
                $basePath = self::getConfigParam('cssPath');
                break;
            case 'js':
                $basePath = self::getConfigParam('jsPath');
                break;
            case 'image':
                $basePath = self::getConfigParam('imagePath');
                break;
            default:
                $basePath = '';
        }
        return Yii::app()->request->baseUrl . "/$basePath/$fileName.$extension";
    }
    
    public static function getForeignUrl($url)
    {
        if (empty($url))
            $url = 'javascript:void(0)';
        elseif (!preg_match ('/http:|https:/', $url))
            $url = 'http://' . $url;
        return $url;
    }
}